package main

import (
	"fmt"
	"github.com/urfave/cli/v2"
	"gomonitor-server/src/broadcast"
	"gomonitor-server/src/config"
	"gomonitor-server/src/db"
	"gomonitor-server/src/http/router"
	"gomonitor-server/src/mylog"
	"gomonitor-server/src/tcp"
	"net/http"
	"os"
	"strconv"
)

func Action(ctx *cli.Context) error {
	// 配置Kubernetes客户端
	appCfg, err := config.InitConfig(ctx)
	if err != nil {
		mylog.Logger.Errorf("加载配置文件失败....")
		return err
	}
	_, err = mylog.LogConfig(&appCfg)
	if err != nil {
		_ = fmt.Errorf("日志初始化失败:%s\n", err.Error())
		return err
	}

	db.InitDataSource(&appCfg.App.DataSource)

	tcp.Start(&appCfg)

	broadcast.StartTask(&appCfg)

	router.Router()
	err = http.ListenAndServe("0.0.0.0:"+strconv.Itoa(appCfg.App.HttpPort), nil) //设置监听的端口
	if err != nil {
		//mylog.Fatal("ListenAndServe: ", err)
		mylog.Logger.Error(err)
	}

	select {}
}

func main() {

	app := new(cli.App)
	app.Name = "gomonitor-server"
	app.Version = "1.0"
	app.Usage = "run"
	app.Action = Action
	app.Flags = []cli.Flag{
		&cli.StringFlag{
			Name:  "cfg",
			Usage: "配置文件路径",
		},
	}
	err := app.Run(os.Args)
	if err != nil {
		mylog.Logger.Error(err)
	}

}
